<?php
namespace bee;

/**
 * @author dzelenika
 */
abstract class form extends singleton
{
  /**
   * @var string
   */
  protected static $__formID = "frm200001010000";
  protected static $__instance;
/**
 * @var XMLDataHub
 */
  protected $__DSDoc;
/**
 * @var dispatcher
 */
  protected $__dispatcher;
/**
 * @var app
 */
  //protected $__app;
  protected $__parent;
  protected $__activeMenu;
/**
 *
 * @var db
 */
  protected $__db;
  /**
   *
   * @var bee\linkManager
   */
  protected $__linkManager;

  public $__caption = "*** C A P T I O N ***";
  public $__meta_desc = "P A G E    D E S C R I P T I O N";
  public $__version = "1.0";
  protected $__template;
  public $__fired;

  /**
   * @return string getter ID-a forme
   */
  public static function getFormID()
  { return static::$__formID;
  }


  /**
   * konstruktor nije vidljiv izvana (sigleton) dohvaća XML dokument izvora HTML-a
   * @return void
   */
  protected function __construct()
  {
    $this->__fired           = false;
    $this->__dispatcher      = dispatcher::getInstance();
    $this->__dispatcher->addObject($this);
    //$this->__DSDoc           = XMLDataHub::getInstance();
    $this->__parent          = isset($_REQUEST["__frm_back"]) ? $_REQUEST["__frm_back"] : "";;
    $this->__withEvents      = true;
    //$this->__app             = app::getInstance();
    //$this->__DSDoc           = XMLDataHub::getInstance();
    $this->__db              = db::getInstance();
    $this->__linkManager     = linkManager::getInstance();
  }

  /**
   * funkcija koja vraća instancu forme (singleton)
   *
   * @param bool $noLoad               ne učitavati formu iz sessije već forsirati kreiranje nove
   * @return bee\form           vraća instancu forme
   */
  public static function getInstance($noLoad = NULL)
  {//die( "GIS " . print_r(array_keys($_SESSION), true));
    $a = app::getInstance();
   // $a->addTimePoint("start of fromGetInstance");
    if($a->request->getField("__ajax"))
    {
      echo ":: " . $a->request->getField("__ajax") . " ::";
    }
    $d = dispatcher::getInstance();


    $className = get_called_class();

    if(($noLoad ?: false) == true)
    { //echo "NOL ";
      static::$__instance = new $className();
      $a->addTimePoint("+ pre createForm");
      $d->dispatch("createForm");
      $a->addTimePoint("+ post createForm");
      $d->dispatch("onCreate");
    } else
    {
      //die("::" . static::$__formID . "::");
      static::$__instance = factory::load(static::$__formID);

      //die("::" . static::$__formID . "::");
      if(static::$__instance)
      { //echo "LOA ";
      } else
      { //echo "FLD";
        static::$__instance = new $className();
        $a->addTimePoint("+ pre createForm");
        $d->dispatch("createForm");
        $a->addTimePoint("+ post createForm");
        $d->dispatch("onCreate");
      }
    }

    /**
     * @todo i ovdje i kod grida se prilikom svakog instanciranja izvršavaju svi eventi
     */
    $a->addTimePoint("+ pre loadForm");
    $d->dispatch("loadForm");
    append2log("//");
    $a->addTimePoint("+ post loadForm");
    //$d->dispatch("onCreate");
    //$d->dispatch("doPreEvents");
    //$d->dispatch("onCreate");-->removed 2012-04-22
    $d->dispatch("onLoad");//???????????????????

    if(isset($_REQUEST["sys_action"]))
    {

      $a->addTimePoint("+ pre sys_action Form");

      $d->dispatch($_REQUEST["sys_action"]);
      //die("10");
      $a->addTimePoint("+ post sys_action Form");
    }

    $d->dispatch("doEvents");

    $a->addTimePoint("++ pre onDraw");

    $d->dispatch("onDraw");

    $a->addTimePoint("++ post onDraw");

    $a->addTimePoint("+ pre drawForm");
    $d->dispatch("drawForm");

    $a->addTimePoint("+ post drawForm");
    //static::$dispatcher->dispatch("createJQueries");

    $d->dispatch("unLoad");
    $d->dispatch("unloadForm");
//die(debug_print_backtrace());
    return static::$__instance;

  }

  public function __wakeup()
  { //app::getInstance()->setDebug("<br />WEJK");
    $a = app::getInstance();
    $a->addTimePoint("+ start of __wake Form");
    $className = get_called_class();
    if($this)
    { //die( "<br />WAKE FAKE (" . $className::$__formID . ")" . get_called_class());
      $this->__fired        = false;
      $this->__dispatcher   = dispatcher::getInstance();
      $this->__dispatcher->addObject($this);
      //$this->__DSDoc        = XMLDataHub::getInstance();
      $this->__db           = db::getInstance();
      //$this->__DSDoc->setArray($_REQUEST, "__request");
      //$this->__app          = app::getInstance();
      $this->__linkManager  = linkManager::getInstance();
      $a->addTimePoint("+ end of __wake Form");

    }
/*
    foreach ($this as $key => &$value)
    {
      if($value instanceof webPane)
      { //@$this->__app->setDebug("<br />" . $key . " - " . get_class($value));
        //$value->__parent = $this;
      }
    }
*/
    static::$__instance = $this;
  }

  public function __sleep__()
  { //app::getInstance()->setDebug("<br />SLIP");
    $arr=array();

    foreach ($this as $key => &$value)
    {
      if($value instanceof webPane)
      {
        //@app::getInstance()->setDebug("<br />" . $key . " - " . get_class($value));
        $value->__parent = NULL;
      }
      $arr[] = $key;
    }
    //app::getInstance()->setDebug(print_r($arr, true));
    return $arr;
  }

  public function doEvents()
  {
    $__app = app::getInstance();

    $__app->activeMenu = $this->__activeMenu;
    if($this->__template)
    { $__app->setTemplate($this->__template);
    }
  //echo " **"  . $this->__template . "** ";
  }

  /**
   *
   * @param array $event
   * mislim da se ne koristi --> mislim da bi draw, unload i close trebalo propagirati gridu i widgetima na formi
   * ovime se izbjegava ponavljanje ovih akcija za svaku formu nego se objekti trebaju znati sami "crtati"
   */
  protected static function dispatchChildren($event)//
  { foreach(static::$__instance as $item)
    { if(is_a($item, "bee\\object"))
      { if($item->__withEvents == true)
        { //echo "#".get_class($item)."#";
          //static::$dispatcher->dispatch($event);
        } else
        { //echo "/".get_class($item)."/";
          //static::$dispatcher->dispatch($event);
        }
      } else
      { //echo is_object($item) ? "=".get_class($item)."=" : "";

      }
    }

  }

  /**
   * funkcija "event handler" učitavanje forme
   * @param array $evt      matrica koja sadrži detalje doga�aja koji je uzrokovao poziv funkcije (obično $_REQUEST)
   * @return void
   */
  public function loadForm($evt)
  { //echo "<br />form::loadForm:" . print_r($evt, true);
  }

  public function drawForm($evt)
  { //echo "<br />form::loadForm:" . print_r($evt, true);
    //static::$dispatcher->dispatch("onDraw");
//die("???????");
    //$this->createJQueries($evt);
  }


	/**
	 *
	 * @param $evt
	 * @return void
	 */
  public function unLoadForm($evt)
  { //$className = get_called_class();
    //$this->__DSDoc->setVariable("__frm_back", $this->__parent);
    //factory::save(static::$__instance, static::$__formID);

    //$this->__DSDoc->setVariable("__frm_back", $this->__parent);
    //factory::save(static::$__instance, static::$__formID);
    //var_dump(static::$__instance);
    //echo "-$this->__formID-";
    //echo "opooooooooooooooooo";
  }

  public function cleanUp()
  { /*
    //app::getInstance()->setDebug("<br />CLEANUP");
    foreach ($this as $key => &$value)
    { if($value instanceof webPane)
      { //@app::getInstance()->setDebug("<br />" . $key . " - " . get_class($value));
        //$value->__parent = NULL;
      }
    }
    */
    //$this->__app           = NULL;
    //$this->__DSDoc         = NULL;
    $this->__dispatcher    = NULL;
  }

  /**
   *
   * @param $evt
   * @return unknown_type
   */
  public function closeForm($evt)
  {
  }

  /**
   *
   * @param $evt
   * @return void
   */
  public function createForm($evt)
  { //echo "<br />form::createForm:" . print_r($evt, true);
  }

  protected function setParentParam($value, $name="sys_action")
  { $found = false;
    $par_arr = explode("&", $this->__parent);
    //print_r($par_arr);
    foreach($par_arr as &$par)
    { $k_v = explode("=", $par);
      if($k_v[0] == $name)
      { $par = "$k_v[0]=$value";
        $found = true;
        break;
      }
    }
    if(!$found)
    { $par_arr[] = "$name=$value";
    }
    $this->__parent = implode("&", $par_arr);
  }

  public function getParent()
  { return $this->__parent;
  }

//   public function createJQueries($evt)
//   { $reflObj = new \ReflectionObject(static::$__instance);

//     $props = $reflObj->getProperties();
//     foreach ($props as $refProperty)
//     { $vn = $refProperty->getName();
//       if(!$refProperty->isStatic() && is_a($this->$vn, "bee\widget") )
//       { $widget = $this->$vn;
//         //echo $refProperty->getName();

//         if($widget->widgetType == 1)
//         { //echo $refProperty->getName() . " ";
//           if($widget->datePicker)
//           {
//             $this->__DSDoc->setObject("jqueries/datePickers/" . $vn , $widget->datePicker);
//           }
//           if($widget->autoComplete)
//           { $this->__DSDoc->setObject("jqueries/autoCompleters/" . $vn , $widget->autoComplete);
//           }
//           //static::$DSDoc->setObject("jqueries/" . $vn, $widget);
//         }
//       }
//     }
//   }
}